package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportVisitsLogRpcService;
import com.hunttown.mes.rpc.domain.TransportVisitsLogDTO;
import com.hunttown.mes.rpc.domain.query.TransportVisitsLogDTOQuery;
import com.hunttown.mes.service.TransportVisitsLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportVisitsLogConverts.convert;

/**
 * created by wangjunfu on 2022-09-16 18:56:08
 */
@Service
public class TransportVisitsLogRpcServiceImpl implements TransportVisitsLogRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportVisitsLogRpcServiceImpl.class);

    private final TransportVisitsLogService transportVisitsLogService;

    @Autowired
    public TransportVisitsLogRpcServiceImpl(TransportVisitsLogService transportVisitsLogService) {
        this.transportVisitsLogService = transportVisitsLogService;
    }

    @Override
    public TransportVisitsLogDTO insert(TransportVisitsLogDTO transportVisitsLogDTO) {
        try {
            return convert(transportVisitsLogService.insert(convert(transportVisitsLogDTO)));
        } catch (Exception e) {
            logger.error("TransportVisitsLog create failed.", e);
            throw new RuntimeException("TransportVisitsLog create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportVisitsLogDTO transportVisitsLogDTO) {
        try {
            return transportVisitsLogService.updateInfoById(convert(transportVisitsLogDTO));
        } catch (Exception e) {
            logger.error("TransportVisitsLog update by id failed.", e);
            throw new RuntimeException("TransportVisitsLog update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportVisitsLogDTOQuery transportVisitsLogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportVisitsLogDTOQuery.getEntId() != null) {
                query.put("w_entId", transportVisitsLogDTOQuery.getEntId());
            }

            if (transportVisitsLogDTOQuery.getW_onlyKey() != null && transportVisitsLogDTOQuery.getW_onlyKey() > 0) {
                query.put("w_onlyKey", transportVisitsLogDTOQuery.getW_onlyKey());
            }

            if (transportVisitsLogDTOQuery.getUpdateVisits() != null) {
                query.put("updateVisits", transportVisitsLogDTOQuery.getUpdateVisits());
            }

            return transportVisitsLogService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportVisitsLog update by query failed.", e);
            throw new RuntimeException("TransportVisitsLog update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportVisitsLogDTO selectById(Integer id) {
        try {
            return convert(transportVisitsLogService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportVisitsLog select by id failed.", e);
            throw new RuntimeException("TransportVisitsLog select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportVisitsLogDTO selectObjByQuery(TransportVisitsLogDTOQuery transportVisitsLogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportVisitsLogDTOQuery.getOnlyKey() != null) {
                query.put("onlyKey", transportVisitsLogDTOQuery.getOnlyKey());
            }

            if (transportVisitsLogDTOQuery.getVisitsYear() != null) {
                query.put("visitsYear", transportVisitsLogDTOQuery.getVisitsYear());
            }

            if (transportVisitsLogDTOQuery.getVisitsMonth() != null) {
                query.put("visitsMonth", transportVisitsLogDTOQuery.getVisitsMonth());
            }

            if (transportVisitsLogDTOQuery.getVisitsDay() != null) {
                query.put("visitsDay", transportVisitsLogDTOQuery.getVisitsDay());
            }

            if (StringUtils.isNotBlank(transportVisitsLogDTOQuery.getShowDate())) {
                query.put("showDate", transportVisitsLogDTOQuery.getShowDate());
            }

            if (StringUtils.isNotBlank(transportVisitsLogDTOQuery.getStartDate())) {
                query.put("startDate", transportVisitsLogDTOQuery.getStartDate());
            }

            if (StringUtils.isNotBlank(transportVisitsLogDTOQuery.getEndDate())) {
                query.put("endDate", transportVisitsLogDTOQuery.getEndDate());
            }

            if (StringUtils.isNotBlank(transportVisitsLogDTOQuery.getOrderBy())) {
                query.put("orderBy", transportVisitsLogDTOQuery.getOrderBy());
            }

            return convert(transportVisitsLogService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportVisitsLog select obj by query failed.", e);
            throw new RuntimeException("TransportVisitsLog select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportVisitsLogDTO> getListForPage(TransportVisitsLogDTOQuery transportVisitsLogDTOQuery) {
        Query query = Query.page(transportVisitsLogDTOQuery.getPn(), transportVisitsLogDTOQuery.getPageSize());

        if (transportVisitsLogDTOQuery.getId() != null && transportVisitsLogDTOQuery.getId() > 0) {
            query.put("id", transportVisitsLogDTOQuery.getId());
        }

        if (transportVisitsLogDTOQuery.getIds() != null && !transportVisitsLogDTOQuery.getIds().equals("")) {
            query.put("ids", transportVisitsLogDTOQuery.getIds());
        }

        if (transportVisitsLogDTOQuery.getOnlyKey() != null) {
            query.put("onlyKey", transportVisitsLogDTOQuery.getOnlyKey());
        }

        if (transportVisitsLogDTOQuery.getVisitsYear() != null) {
            query.put("visitsYear", transportVisitsLogDTOQuery.getVisitsYear());
        }

        if (transportVisitsLogDTOQuery.getVisitsMonth() != null) {
            query.put("visitsMonth", transportVisitsLogDTOQuery.getVisitsMonth());
        }

        if (transportVisitsLogDTOQuery.getVisitsDay() != null) {
            query.put("visitsDay", transportVisitsLogDTOQuery.getVisitsDay());
        }

        //区间日期
        if (StringUtils.isNotBlank(transportVisitsLogDTOQuery.getDaterange())) {
            String[] date = transportVisitsLogDTOQuery.getDaterange().split("~");
            if (date.length == 2) {
                query.put("startDate", date[0].trim());
                query.put("endDate", date[1].trim());
            }
        }

        if (StringUtils.isNotBlank(transportVisitsLogDTOQuery.getShowDate())) {
            query.put("showDate", transportVisitsLogDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportVisitsLogDTOQuery.getStartDate())) {
            query.put("startDate", transportVisitsLogDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportVisitsLogDTOQuery.getEndDate())) {
            query.put("endDate", transportVisitsLogDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportVisitsLogDTOQuery.getOrderBy())) {
            query.put("orderBy", transportVisitsLogDTOQuery.getOrderBy());
        }

        List<TransportVisitsLogDTO> list = convert(transportVisitsLogService.selectByQuery(query));
        Page<TransportVisitsLogDTO> page = Page.from(transportVisitsLogDTOQuery.getPn(), transportVisitsLogDTOQuery.getPageSize(), list);
        page.setTotalItems(transportVisitsLogService.countByQuery(query));

        return page;
    }

    @Override
    public Long getTotalVisits(TransportVisitsLogDTOQuery transportVisitsLogDTOQuery) {
        try {
            Query query = Query.noPage();

            if (StringUtils.isNotBlank(transportVisitsLogDTOQuery.getStartDate())) {
                query.put("startDate", transportVisitsLogDTOQuery.getStartDate());
            }

            if (StringUtils.isNotBlank(transportVisitsLogDTOQuery.getEndDate())) {
                query.put("endDate", transportVisitsLogDTOQuery.getEndDate());
            }

            return transportVisitsLogService.getTotalVisits(query);
        } catch (Exception e) {
            logger.error("TransportVisitsLog select obj by query failed.", e);
            throw new RuntimeException("TransportVisitsLog select obj by query failed, Please order your code.", e);
        }
    }
}